package demon2;

public class MylinkList implements IMyLinkList{
    class ListNode{
        private int val;
        private ListNode next;
        public ListNode(int val){
            this.val=val;
        }
    }
    public ListNode head;
    public ListNode findIndexSubOne(int index){
        ListNode cur=this.head;
        int count=0;
        while(count==index-1){
            cur=cur.next;
        }
        return cur;
    }
    public void checkIndex(int index){
        if(index<0||index>size()){
            throw new IndexException("index位置不合法");
        }
    }
    @Override
    public void addFirst(int data) {
        ListNode listNode=new ListNode(data);
        listNode.next=this.head;
        this.head=listNode;
    }

    @Override
    public void addLast(int data) {
        ListNode listNode=new ListNode(data);
        ListNode cur=head;
        if(this.head==null){
            this.head=listNode;
        }
        while(cur.next!=null){
            cur=cur.next;
        }
        cur.next=listNode;
    }

    @Override
    public void addIndex(int index, int data) {
        try{
            checkIndex(index);
        }catch (IndexException e){
            e.printStackTrace();
        }
        ListNode listNode=new ListNode(data);
        if(this.head==null){
            addFirst(data);
        }
        if(index==size()){
            addLast(data);
        }
        ListNode cur=findIndexSubOne(index);
        listNode.next=cur.next;
        cur.next=listNode;
    }

    @Override
    public boolean contains(int key) {
        if(this.head==null){
            return false;
        }
        ListNode cur=this.head;
        while(cur!=null){
            if(cur.val==key){
                return true;
            }
            cur=cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        if(this.head==null){
            return;
        }
        if(this.head.val==key){
            this.head=this.head.next;
        }
        ListNode cur=this.head;
        while(cur!=null){
            if(cur.next.val==key){
                ListNode del=cur.next;
                cur.next=del.next;
                return;
            }
            cur=cur.next;
        }
    }

    @Override
    public void removeAllKey(int key) {
        ListNode cur=this.head.next;
        ListNode prve=this.head;
        while(cur!=null){
            if(cur.val==key){
                prve.next=cur.next;
            }else {
                prve=cur;
            }
            cur=cur.next;
        }
        if(this.head.val==key){
            this.head=this.head.next;
        }

    }

    @Override
    public int size() {
        int count =0;
        ListNode cur=head;
        while (cur!=null){
            count++;
            cur=cur.next;
        }
        return count;
    }

    @Override
    public void clear() {
        this.head=null;
    }

    @Override
    public void display() {
        ListNode cur=this.head;
        while(cur!=null){
            System.out.print(cur.val+" ");
            cur=cur.next;
        }
    }
}
